﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscoveryLogic.DataServices.DataSources;
using DiscoveryLogic.UI.Layouts.Controls;
using DiscoveryLogic.UI.Layouts.Converters;

namespace DiscoveryLogic.UI.Layouts.Controls
{
    public partial class HelocBarChartCylinders : UserControl, IGraphControl
    {
        #region Private Variables

            private Account model;

            private const double labelToCylinderDist = 51;

            double taxValueOfHouse = 0;
            double maxDrawAmount = 0;

            private double interestRate;
            private double minInterestRate;
            private double maxInterestRate;
            private double taxBenefit;
            private double minTaxBenefit;
            private double maxTaxBenefit;
            private double monthlyPayment;
            private double minMonthlyPayment;
            private double maxMonthlyPayment;

            private double minPaymentCylinderHeight = 265.986;
            private double maxPaymentCylinderHeight = 200.986;
            private double minInterestCylinderHeight = 400;
            private double maxInterestCylinderHeight = 352.667;
            private double minTaxCylinderHeight = 442.667;
            private double maxTaxCylinderHeight = 342;
            

        #endregion

        #region Constructors

            public HelocBarChartCylinders()
            {
                InitializeComponent();
            }

        #endregion

        #region Get/Set Properties

            public Account Model
            {
                get { return model; }
                set {
                    model = value;
                    InitializeBars();
                }
            }

        #endregion

        #region Helper Methods

            private void SetBars(double drawAmount)
            {
                if(model == null){return;}

                // Set Monthly Payment Cylinder
                Rectangle PaymentBarrel = PaymentCylinderBarrel;
                Ellipse PaymentTop = PaymentCylinderTop;
                Ellipse PaymentBase = PaymentCylinderBase;
                Ellipse PaymentReflection = PaymentCylinderTopReflect;                

                monthlyPayment = ChangeInMonthlyPayment(drawAmount);

                double newTopPosition = minPaymentCylinderHeight
                                        - ((monthlyPayment / ChangeInMonthlyPayment(maxDrawAmount)) 
                                            * Math.Abs(minPaymentCylinderHeight - maxPaymentCylinderHeight));

                PaymentTop.SetValue(Canvas.TopProperty, newTopPosition - PaymentTop.Height / 2);
                PaymentReflection.SetValue(Canvas.TopProperty, newTopPosition - PaymentReflection.Height / 2);

                PaymentBarrel.Height = Math.Abs(newTopPosition - ((double)PaymentBase.GetValue(Canvas.TopProperty) + PaymentBase.Height / 2));
                PaymentBarrel.SetValue(Canvas.TopProperty, newTopPosition);

                PaymentLabel.SetValue(Canvas.TopProperty, newTopPosition - labelToCylinderDist);
                PaymentLabel.Text = Math.Round(minMonthlyPayment + monthlyPayment).ToCurrency();

                // Set Interest Cylinder
                Rectangle InterestBarrel = InterestCylinderBarrel;
                Ellipse InterestTop = InterestCylinderTop;
                Ellipse InterestBase = InterestCylinderBase;
                Ellipse InterestReflection = InterestCylinderTopReflect;

                interestRate = ChangeInInterestRate(drawAmount);

                double newInterestTopPosition = maxInterestCylinderHeight
                                        + ((interestRate / ChangeInInterestRate(maxDrawAmount))
                                            * Math.Abs(minInterestCylinderHeight - maxInterestCylinderHeight));

                InterestTop.SetValue(Canvas.TopProperty, newInterestTopPosition - InterestTop.Height / 2);
                InterestReflection.SetValue(Canvas.TopProperty, newInterestTopPosition - InterestReflection.Height / 2);

                InterestBarrel.Height = Math.Abs(newInterestTopPosition 
                                                 - ((double)InterestBase.GetValue(Canvas.TopProperty) 
                                                            + InterestBase.Height / 2));
                InterestBarrel.SetValue(Canvas.TopProperty, newInterestTopPosition);

                InterestLabel.SetValue(Canvas.TopProperty, newInterestTopPosition - labelToCylinderDist);
                InterestLabel.Text = Math.Round(maxInterestRate - interestRate,2).ToString() + "%";

                // Set Tax Benefit Cylinder
                Rectangle TaxBarrel = TaxCylinderBarrel;
                Ellipse TaxTop = TaxCylinderTop;
                Ellipse TaxBase = TaxCylinderBase;
                Ellipse TaxReflection = TaxCylinderTopReflect;

                taxBenefit = ChangeInTaxBenefit(drawAmount);

                double newTaxTopPosition = minTaxCylinderHeight
                                        - ((taxBenefit / ChangeInTaxBenefit(maxDrawAmount))
                                            * Math.Abs(minTaxCylinderHeight - maxTaxCylinderHeight));

                TaxTop.SetValue(Canvas.TopProperty, newTaxTopPosition - TaxTop.Height / 2);
                TaxReflection.SetValue(Canvas.TopProperty, newTaxTopPosition - TaxReflection.Height / 2);

                TaxBarrel.Height = Math.Abs(newTaxTopPosition - ((double)TaxBase.GetValue(Canvas.TopProperty) + TaxBase.Height / 2));
                TaxBarrel.SetValue(Canvas.TopProperty, newTaxTopPosition);

                TaxLabel.SetValue(Canvas.TopProperty, newTaxTopPosition - labelToCylinderDist);
                TaxLabel.Text = Math.Round(taxBenefit).ToCurrency();
            }

            private void InitializeBars()
            {
                taxValueOfHouse = ((MortgageAccount)model).TaxValueHouse;
                maxDrawAmount = Math.Round(taxValueOfHouse * 0.8);

                minMonthlyPayment = model.NextPaymentAmount;
                maxMonthlyPayment = minMonthlyPayment
                                    + ChangeInMonthlyPayment(maxDrawAmount);
                
                maxInterestRate = ((MortgageAccount)model).InterestRate;
                minInterestRate = maxInterestRate
                                  - ChangeInInterestRate(maxDrawAmount);
                
                minTaxBenefit = 0;
                maxTaxBenefit = minTaxBenefit
                                + ChangeInTaxBenefit(maxDrawAmount);
            }

            private double ChangeInMonthlyPayment(double drawAmount)
            {
                return drawAmount / 10 / 12;
            }

            private double ChangeInInterestRate(double drawAmount)
            {
                double taxValueOfHouse = ((MortgageAccount)model).TaxValueHouse;
                double maxDrawAmount = Math.Round(taxValueOfHouse * 0.8);

                return 2 * (drawAmount/maxDrawAmount);
            }

            private double ChangeInTaxBenefit(double drawAmount)
            {
                return drawAmount / 5;
            }

        #endregion

        #region Public Methods

            public void SetBarHeight(double drawAmount)
            {
                SetBars(drawAmount);
            }

        #endregion

        #region IGraphControl Members

            public void SetSlider(double delta)
            {
                throw new NotImplementedException();
            }

            public event SliderChangedEventHandler SliderChanged;

            public void OnSliderChanged(EventArgs e)
            {
                throw new NotImplementedException();
            }

            public double GetLastSliderChange()
            {
                throw new NotImplementedException();
            }

            public double GetCurrentPosition()
            {
                throw new NotImplementedException();
            }

            public double GetSliderLength()
            {
                throw new NotImplementedException();
            }

            public void SetSliderPercent(double percent)
            {
                throw new NotImplementedException();
            }

        #endregion

    }
}
